using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Rensoft.CRM.Client.Properties;

namespace Rensoft.CRM.Client
{
    public partial class ContactSearch : Form
    {
        private bool searchPending;
        private bool searchEnabled;

        public List<string> GuidList
        {
            get
            {
                List<string> guidList = new List<string>();
                foreach (ListViewItem item in resultsListView.SelectedItems)
                {
                    guidList.Add((string)item.Tag);
                }
                return guidList;
            }
        }

        protected delegate void AddSearchResult(string name, string company, string guid);

        public ContactSearch()
        {
            InitializeComponent();
            Text = "Contact Search - " + AssemblyInfo.Title;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            resultsListView.Select();
        }

        void hiddenSearchButton_Click(object sender, EventArgs e)
        {
            performSearchAsync();
        }

        private void defaultSearchTextBox()
        {
            searchTextBox.ResetText();
            searchTextBox.ForeColor = SystemColors.GrayText;
        }

        private void performSearchAsync()
        {
            if (searchBackgroundWorker.IsBusy)
            {
                searchPending = true;
                searchBackgroundWorker.CancelAsync();
                return;
            }

            Cursor = Cursors.AppStarting;
            searchBackgroundWorker.RunWorkerAsync();
        }

        private void addSearchResult(string name, string company, string guid)
        {
            ListViewItem item = resultsListView.Items.Add(
                new ListViewItem(new string[] { name, company }));
            item.Tag = guid;
        }

        private void searchBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (searchPending)
            {
                searchBackgroundWorker.RunWorkerAsync();
                return;
            }

            Cursor = Cursors.Default;
        }

        private void resultsListView_DoubleClick(object sender, EventArgs e)
        {
            _acceptButton.PerformClick();
        }

        private void resultsListView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control & (e.KeyCode == Keys.A))
            {
                foreach (ListViewItem item in resultsListView.Items)
                {
                    item.Selected = true;
                }
            }
            else if (e.KeyCode == Keys.Enter)
            {
                _acceptButton.PerformClick();
            }
        }

        private void searchTextBox_TextChanged(object sender, EventArgs e)
        {
            if (searchEnabled)
            {
                searchTimer.Stop();
                searchTimer.Start();
            }
        }

        private void searchTimer_Tick(object sender, EventArgs e)
        {
            if (!searchBackgroundWorker.IsBusy)
            {
                searchTimer.Stop();
                performSearchAsync();
            }
        }

        private void searchTextBox_Leave(object sender, EventArgs e)
        {
            searchEnabled = false;
            if (string.IsNullOrEmpty(searchTextBox.Text))
            {
                defaultSearchTextBox();
            }
        }

        private void searchTextBox_Enter(object sender, EventArgs e)
        {
            if (searchTextBox.Text == "Search Contacts")
            {
                clearSearchTextBox();
            }
            searchEnabled = true;
        }

        private void clearSearchTextBox()
        {
            searchTextBox.Text = string.Empty;
            searchTextBox.ForeColor = SystemColors.WindowText;
        }
    }
}